package com.example.all4.condition;

import lombok.Data;

import java.util.Map;
import java.util.function.Function;

@Data
public abstract class AbstractSCondition implements SCondition {

    private Integer type;
    private String name;
    private boolean isTrue = false;
    private Map<String,Object> param;
    private Function<Map<String,Object>,Boolean> conditionEvent;
    private Function<Map<String,Object>,Map<String,Object>> trueEvent;
    private Function<Map<String,Object>,Map<String,Object>> falseEvent;
    private SCondition isTrueNext;
    private SCondition isFalseNext;
    private Boolean isCanReturn  = false;
    private Map<String,Object> resultMap;

    private AbstractSCondition(Integer type,String name, Map<String,Object> param){
        this.type = type;
        this.name = name;
        this.param = param;
    }

    private AbstractSCondition(Integer type, String name){
        this(type,name,null);
    }

    public AbstractSCondition (Integer type, Map<String,Object> param){
        this(type,"",param);
    }

    @Override
    public void setCanReturn() {
        this.isCanReturn = true;
    }

    @Override
    public boolean canReturn() {
        return this.isCanReturn;
    }

    @Override
    public boolean isFalse() {
        return this.isTrue;
    }
    @Override
    public boolean isTrue(){
        return this.isTrue;
    }

    @Override
    public Boolean hasNext() {
        if(this.isTrueNext != null || this.isFalseNext != null){
            return true;
        }
      return false;
    }

    @Override
    public SCondition nextTrue(SCondition trueCondition) {
        this.isTrueNext = trueCondition;
        return this;
    }
    @Override
    public SCondition nextFalse(SCondition falseCondition) {
        this.isFalseNext = falseCondition;
        return this;
    }

    @Override
    public void bind(Function<Map<String, Object>, Boolean> function) {
        this.conditionEvent = function;
    }

    @Override
    public void bindTrueEvent(Function<Map<String,Object>,Map<String,Object>> trueEvent) {
        this.trueEvent =trueEvent;
    }

    @Override
    public void bindFalseEvent(Function<Map<String, Object>, Map<String,Object>> falseEvent) {
        this.falseEvent = falseEvent;
    }

    @Override
    public void execute() {
        this.isTrue = this.conditionEvent.apply(this.param);
    }
    @Override
    public SCondition executeTrueEvent() {
        if(this.trueEvent != null){
            this.resultMap = this.trueEvent.apply(this.param);
        }
        return this;
    }
    @Override
    public SCondition executeFalseEvent() {
        if(this.falseEvent != null){
            this.resultMap = this.falseEvent.apply(this.param);
        }
        return this;
    }
    @Override
    public SCondition getTrueNext(){
        return isTrueNext;
    }
    @Override
    public SCondition getFalseNext(){
        return isFalseNext;
    }

}
